<?php

require_once '../include/common.inc.php';
require_once '../include/upfile.class.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

$menu_index = 2;
$page_title = 'JANSEN';
$guide_message = 'Customer | Browsing Tracking';
$out_ary = array();
$alert_message = '';

$key = "Enter subject...";
getMyParam('action');

if ($action == "search") {
    //echo "<pre>";print_r($_POST);exit;
    getMyParam('day1');
    getMyParam('month1');
    getMyParam('year1');
    getMyParam('day2');
    getMyParam('month2');
    getMyParam('year2');

/*    $now_h = date('H', $timestamp);
    $now_i = date('i', $timestamp);
    $now_s = date('s', $timestamp);
    
    $start_date = mktime($now_h, $now_i, $now_s, $month1, $day1, $year1);
    $end_date = mktime($now_h, $now_i, $now_s, $month2, $day2, $year2);*/
	$start_date = mktime(0, 0, 0, $month1, $day1, $year1);
    $end_date = mktime(23, 59, 59, $month2, $day2, $year2);
} 
else if($action == "listall")
{
	$sql = "select date FROM `browse_tracking` ORDER BY date LIMIT 0,1";
	$rs = $db->get_one($sql);
	$start_date = $rs["date"];
	
    $sql = "select date FROM `browse_tracking` ORDER BY date DESC LIMIT 0,1";
	$rs = $db->get_one($sql);
	$end_date = $rs["date"];
	
}
else {	
    $end_date = $timestamp;
	$start_date = $timestamp-3600*24;
	$start_date = mktime(0, 0, 0, date('m', $start_date), date('d', $start_date), date('Y', $start_date));
}

$start_select_year = date('Y', $start_date);
$start_select_month = date('m', $start_date);
$start_select_day = date('d', $start_date);
$end_select_year = date('Y', $end_date);
$end_select_month = date('m', $end_date);
$end_select_day = date('d', $end_date);


// ================ start =======================

if($start_date == $end_date)
{
	$search_date = mktime(0,0,0, $month1, $day1, $year1);
	$sql_where = " WHERE bt.`date` >= '$search_date' AND  bt.`date` < '".($search_date+3600*24)."' AND bt.`action`=1";
}
else 	
	$sql_where = " WHERE bt.`date` >= '$start_date' AND bt.`date` <= '$end_date' AND bt.`action`=1";
//debug($sql_where);
$page_url = "c_browsing_tracking.php?action=$action&day1=$start_select_day&month1=$start_select_month&year1=$start_select_year&day2=$end_select_day&month2=$end_select_month&year2=$end_select_year&";

/*
if ($action == "search" && trim($key) != "") {
    $sql_where .= " AND (`username` LIKE '%$key%' OR `email` LIKE '%$key%')";
    $page_url = 'user_record.php?action=search&key=' . $key . '&';
}
*/

// *** page code start ***
$sql = "SELECT bt.id, bt.uid FROM `browse_tracking` bt $sql_where";
$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);

// ?categoryinventory
if ($sql_all_num > 0) {
    $sql = "SELECT `id`, `name` FROM `category`";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $categroy_ary[$rs['id']] = $rs['name'];
    }
    $categroy_ary[1001] = 'New Items';
    
    $sql = "SELECT `id`, `name` FROM `inventory`";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $inventory_ary[$rs['id']] = $rs['name'];
    }
}

$ppp = 20;
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}

$page_show = getPageStr($sql_all_num, $page, $ppp, $page_url);

$product_id_ary = array();
$product_ary = array();
$code_ary = array();

$sql = "SELECT  u.`id`, u.`username`, u.`phone`, u.`email`, u.`countryid`,bt.`date`,u.`from` FROM `browse_tracking` bt left join  `user` u ON bt.uid = u.id $sql_where ORDER BY bt.`date` DESC LIMIT $start_limit, $ppp";
//debug($sql);
/*$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    if ($rs['action'] == 3 || $rs['action'] == 4) {
        $product_id_ary[] = $rs['aid'];
    }
}*/
//debug($rs);

/*if (!empty($product_id_ary)) {
    $product_id_str = implode(",", $product_id_ary);
    $sql_tmp = "select `id` ,`name`, `code` from `product` where `id` IN (" . $product_id_str . ")";
    $query_tmp = $db->query($sql_tmp);
    while($rs_tmp = $db->fetch_array($query_tmp)) {        
        $product_ary[$rs_tmp['id']] = $rs_tmp['name'];        
        $code_ary[$rs_tmp['id']] = $rs_tmp['code'];        
    }
}*/


$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    /*switch ($rs['action']) {
        case 1:
            $details = '';
            break;
        case 2:
            $details = '';
            break;
        case 3:
            $id_str = $rs['details'];
            if (empty($id_str)) {
                    $details = 'Products>' . $product_ary[$rs['aid']] . '(' . $code_ary[$rs['aid']] . ')';
            } elseif (strstr($id_str, '||')) {
                list($id1, $id2) = explode("||", $id_str);
                $details = 'Products>' . $categroy_ary[$id1] . '>' . $categroy_ary[$id2] . '>' . $product_ary[$rs['aid']] . '(' . $code_ary[$rs['aid']] . ')';
            } else {
                $id1 = $id_str;
                $details = 'Products>' . $categroy_ary[$id1] . '>' . $product_ary[$rs['aid']] . '(' . $code_ary[$rs['aid']] . ')';
            }
            break;
        case 4:
            $id_str = $rs['details'];
            if (strstr($id_str, '||')) {
                list($id1, $id2) = explode("||", $id_str);
                $details = 'Inventory>' . $inventory_ary[$id1] . '>' . $categroy_ary[$id2] . '>' . $product_ary[$rs['aid']] . '(' . $code_ary[$rs['aid']] . ')';
            } else {
                $id1 = $id_str;
                $details = 'Inventory>' . $inventory_ary[$id1] . '>' . $product_ary[$rs['aid']] . '(' . $code_ary[$rs['aid']] . ')';
            }
            break;
        case 5:
            $details = 'Tradeshow>' . $rs['details'];
            break;
        case 6:
            $details = "Key: " . $rs['details'];
            break;
        case 7:
            $details = $rs['details'];
            break;
        default:
            $details = $rs['details'];
            break;
    }*/
	//$dns=array('CN'=>"jansenfurniture.cn<br/>jansenfurniture.com.cn",'EN'=>'jansenfurniture.com');
   $dns=array('CN'=>"CN",'EN'=>'EN');
	$out_ary[] = array('date'=>date('Y-m-d', $rs['date']),'usid'=>$rs['id'], 'time'=>date('H:i:s', $rs['date']), 'name'=>$rs['username'], 'contact'=>'Email:'.$rs['email'].'&nbsp;,&nbsp;Tel:'.$rs['phone'],'country'=>$COUNTRY_ARY[$rs['countryid']],'From'=>$dns[$rs['from']]);
}

$start_year_out=$start_month_out=$start_day_out = '';
$end_year_out=$end_month_out=$end_day_out = '';

for($i=2007; $i<2014; $i++) {
    if ($i == $start_select_year) {
        $start_year_out .= '<option value="' . $i . '" selected>' . $i . '</option>';
    } else {
        $start_year_out .= '<option value="' . $i . '">' . $i . '</option>';
    }

    if ($i == $end_select_year) {
        $end_year_out .= '<option value="' . $i . '" selected>' . $i . '</option>';
    } else {
        $end_year_out .= '<option value="' . $i . '">' . $i . '</option>';
    }

}

for($i=1; $i<13; $i++) {
    if ($i == $start_select_month) {
        $start_month_out .= '<option value="' . $i . '" selected>' . $i . '</option>';
    } else {
        $start_month_out .= '<option value="' . $i . '">' . $i . '</option>';
    }

    if ($i == $end_select_month) {
        $end_month_out .= '<option value="' . $i . '" selected>' . $i . '</option>';
    } else {
        $end_month_out .= '<option value="' . $i . '">' . $i . '</option>';
    }
}

for($i=1; $i<32; $i++) {
    if ($i == $start_select_day) {
        $start_day_out .= '<option value="' . $i . '" selected>' . $i . '</option>';
    } else {
        $start_day_out .= '<option value="' . $i . '">' . $i . '</option>';
    }

    if ($i == $end_select_day) {
        $end_day_out .= '<option value="' . $i . '" selected>' . $i . '</option>';
    } else {
        $end_day_out .= '<option value="' . $i . '">' . $i . '</option>';
    }
}

$debug = 1;
debuginfo();

//debug($out_ary);
include_once(JANSEN_ROOT . 'admin/menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_c_browsing_tracking');
require_once PrintEot('a_footer');

?>